<?php
session_start();
$raizController = dirname(__FILE__);

require_once "$raizController/../config.php";

$em->getConnection()->beginTransaction();

$dql = "select r from Entities\Recibo r JOIN r.referencias ref order by r.id";

$recibos = $em->createQuery($dql)->getResult();


foreach ($recibos as $r){
	echo "{$r->getId()} <br />";
	$total = 0;
	$acrescimo = 0;
	$valor = 0;
	$dinheiro = 0;
	$cheque = 0;
	$cartao = 0;
	
	$referencias = $r->getReferencias();
	
	foreach ($referencias as $referencia) {
		$valor += $referencia->getValor();
		$dinheiro += $referencia->getDinheiro();
		$cheque += $referencia->getCheque();
		$cartao += $referencia->getCartao();
		$total += $referencia->getTotal();
		$acrescimo += $referencia->getAcrescimo();
		
	}
	
	$r->setValor($valor);
	$r->setAcrescimo($acrescimo);
	$r->setCartao($cartao);
	$r->setTotal($total);
	$r->setDinheiro($dinheiro);
	$r->setCheque($cheque);
	
	$em->persist($r);
	
}


try {
	$em->flush();
	$em->getConnection()->commit();
} catch (Exception $e) {
	echo $e->getMessage();
	$em->getConnection()->rollback();
}